import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @Author zhang lei
 * @Date 2021-11-05 17:48
 */
public class Ali {
    public static void main(String[] args) {
//        Semaphore s = new Semaphore(3);
//        int num = 10;
//        for (int i = 0; i < 3; i++) {
//            new Thread(new seller(s, num), "卖线程").start();
//        }
//
//        for (int i = 0; i < 3; i++) {
//            new Thread(new adder(s, num), "加线程").start();
//        }

//        int[] prices1 = {1,2,3,4,5,6};
//        int[] prices2 = {9,2,3,10,5,6};
//
//        System.out.println(block3(prices1));
//        System.out.println(block3(prices2));

        int[] arr = {1,4};
        System.out.println(findPoisonedDuration(arr, 2));
    }

    private static int block3(int[] prices1) {
        int low = Integer.MAX_VALUE;
        int res = 0;

        for (int i = 0; i < prices1.length; i++) {
            low = Math.min(low, prices1[i]);
            res = Math.max(res, prices1[i] - low);
        }
        return res;
    }

    public static int findPoisonedDuration(int[] timeSeries, int duration) {
        //创建给数组，arr[i]表示 第i秒的攻击持续到时长
        int beforeDura = timeSeries[1] + duration -1;
        int result = duration;
        for(int i = 1; i<timeSeries.length; i++) {

            int cur = timeSeries[i];
            int curDura = cur + duration - 1;

            if(cur < beforeDura) {
                result = result + curDura - beforeDura;
            }else {
                result = result + duration;
            }
            beforeDura = curDura;
        }
        return result;
    }


    public static int block(int[] prices) {
        int max = 0;

        for (int i = 0; i < prices.length; i++) {
            int now = prices[i];
            for (int j = i+1; j < prices.length; j++) {
                max = Math.max(max, prices[j]-now);
            }
        }
        return max;
    }

    public static int block2(int[] prices) {
        int max = 0;
        int[] dp = new int[prices.length];
        for (int i = 1; i < prices.length; i++) {
            int cur = prices[i];
            for (int j = i-1; j >=0; j--) {
                if(cur > prices[j]) {
                    dp[i] = dp[j] + (cur - prices[j]);
                    max = Math.max(max, dp[i]);
                    break;
                }
            }
        }

        return max;
    }

}

class adder implements Runnable{

    private Semaphore semaphore;

    private Integer sum;

    public adder(Semaphore s, Integer sum) {
        this.semaphore = s;
        this.sum = sum;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (sum < 10 && sum>0) {
                    semaphore.acquire();
                    sum++;
                    semaphore.release();
                    System.out.println(Thread.currentThread().getId() + "---库存加1:---"+ sum);
                }else {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getId() + "休息1s" + sum);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class seller implements Runnable{

    private Semaphore semaphore;

    private Integer sum;

    public seller(Semaphore s, Integer sum) {
        this.semaphore = s;
        this.sum = sum;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if(sum>0) {
                    semaphore.acquire();
                    sum--;
                    System.out.println(Thread.currentThread().getId() + "---库存减1---"+ sum);
                    semaphore.release();
                }else {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getId() + "休息1s" + sum);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
